﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Code.BLL;
using QianBuo.Lottery.Web.Models;
using Code.Models;
using System.Web.Security;
using Code.Commen;

namespace QianBuo.Lottery.Web.Controllers
{
    public class SharedController : BaseController
    {
        //
        // GET: /Shared/

        public PartialViewResult Top()
        {
            var lotteryType = CacheManage.Lottery.OrderBy(p => p.Value.ID).OrderBy(p => p.Value.Sort).Where(p => p.Value.State == (short)LotteryState.Normal).ToNameValue(p => p.Key.ToString(), p => p.Value.Name);

            TopModel model = new TopModel()
            {
                Lotterys = new ModelNameValue<string, string>(),
                Ratios = new ModelNameValue<int, string>(),
                WinModels = new ModelNameValue<int, string>(),
                Thems = Global.Thems,
            };
            model.Lotterys.AddRange(lotteryType);
           
            model.Ratios.AddRange(CacheManage.Ratio.Where(p => p.Value.State == (short)ConsumeRatioState.Normal).OrderByDescending(p => p.Value.Ratio).ToNameValue(p => p.Value.ID, p => p.Value.Name));
            List<WinMoneyModel> ls = PlayTypeOptionManage.SelectIdFromPlayTypeOption(LoginUserID);
          
            foreach (var item in ls)
            {
                model.WinModels.Add(item.ID,item.Name);
            }
            model.Ratio = CacheManage.Ratio.First(p => p.Value.Ratio == base.LoginUser.Ratio).Value.ID;
            model.WimModel = base.LoginUser.WinModelID;
            return PartialView(model);
        }

        public PartialViewResult ToolBar(bool? ShowToolBar)
        {
            if (Request.IsAjaxRequest())
                Session["ShowToolBar"] = ShowToolBar;
            var accm = new AddChildConfigManage();
            var temp = accm.GetNowAddChildConfig(LoginUserID);
            var up = new UpRankManager();
            var sa = up.SetcteUpRank(LoginUserID);
            var model = new UserInfoModel
            {
                RatioName = CacheManage.Ratio[base.LoginUser.Ratio].Name,

                WinModelName = CacheManage.WinModel[base.LoginUser.WinModelID].Name,
                LoginName = base.LoginUser.LoginName,
                Balance = base.LoginUser.Balance,
                Freez = base.LoginUser.FreezeMoney,
                IsFreez = LoginUser.State == UserState.Freez,
                GuanRank = (Code.BLL.CacheManage.OptionSite[Code.Models.Option_Site_Key.Site_Cache_Rating].ToBool(false)),
                Rans = sa.Rank,
                ThisEx = sa.ThisEx,
                Ex = sa.RankEx,
                Ex1 = up.SetcteUpRank1(LoginUserID),
                ChildCount = temp
            };
            model.EnableAddChildConfig = CacheManage.OptionSite[Option_Site_Key.Site_AddChildConfig_Enable].ToBool(false);

            return PartialView(model);
        }

        public ActionResult Clear()
        {
            return Redirect(Request["ReturnUrl"] ?? Url.Action("", ""));
        }

        public PartialViewResult Left()
        {
            var model = new LeftModel
            {
                LoginName = base.LoginUser.LoginName,
                Balance = base.LoginUser.Balance,
                Freez = base.LoginUser.FreezeMoney,
                RatioName = CacheManage.Ratio[base.LoginUser.Ratio].Name,
                WinModelName = CacheManage.WinModel[base.LoginUser.WinModelID].Name,
                IsFreez = LoginUser.State == UserState.Freez
            };

            return PartialView(model);
        }

        public PartialViewResult Footer()
        {
            var model = new FooterModel
            {
                Thems = Global.Thems
            };
            return PartialView(model);
        }

        public ActionResult Them(string id)
        {
            var them = Global.Thems.First(p => p == id);

            if (them != null)
            {
                base.LoginUser.Them = them;
                base.SetAuthTicket(LoginUser);
            }


            if (Request["ReturnUrl"] != null)
            {
                return Redirect(Request["ReturnUrl"]);
            }

            return RedirectToAction("", "");
        }

        public ActionResult Ratio(int id)
        {
            var ratio = CacheManage.Ratio.Values.FirstOrDefault(p => p.ID == id);
            if (ratio != null)
            {
                base.LoginUser.Ratio = ratio.Ratio;
                base.SetAuthTicket(LoginUser);
            }


            if (Request["ReturnUrl"] != null)
            {
                return Redirect(Request["ReturnUrl"]);
            }

            return RedirectToAction("", "");
        }

        public ActionResult WinModel(int id)
        {

            var winmodel = CacheManage.WinModel.Values.FirstOrDefault(p => p.ID == id);
            if (winmodel != null)
            {
              
                base.LoginUser.WinModelID = winmodel.ID;
                base.SetAuthTicket(LoginUser);
            }

            if (Request["ReturnUrl"] != null)
            {
                return Redirect(Request["ReturnUrl"]);
            }

            return RedirectToAction("", "");
        }

        public ActionResult Out()
        {
            if (!User.IsInRole(AuthorityKey.Manage_User_Login.ToString()))
            {
                Global.OnLineUsers.Remove(LoginUserID);
            }
            Session.Abandon();
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Login");
        }

    }
}
